package com.company.unspecific;

public class IntToRoman {
    /**
     * 罗马数字包含以下七种字符：I，V，X，L，C，D和M。
     * 字符          数值
     * I             1
     * V             5
     * X             10
     * L             50
     * C             100
     * D             500
     * M             1000
     *
     * I可以放在V(5) 和X(10) 的左边，来表示 4 和 9。
     * X可以放在L(50) 和C(100) 的左边，来表示 40 和90。
     * C可以放在D(500) 和M(1000) 的左边，来表示400 和900。
     */
    public String intToRoman(int n) {

        //首先 按每一位进行拆分  个 十 百 千.....
        int onePlaces = n % 10;//个位
        int tenPlaces = (n / 10) % 10;//十位
        int hundredPlaces = (n / 100) % 10;
        int thousandPlaces = (n / 1000) % 10;//千位

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(transitThousandPlace(thousandPlaces));
        stringBuilder.append(transitHandredPlace(hundredPlaces));
        stringBuilder.append(transitTenPlace(tenPlaces));
        stringBuilder.append(transitOnePlace(onePlaces));
        String result = stringBuilder.toString();
        System.out.println(n+"  :   "+result);
        return result;
    }

    private String transitOnePlace(int n) {
        //以5 做区分
        return switch (n) {
            case 1 -> "I";
            case 2 -> "II";
            case 3 -> "III";
            case 4 -> "IV";
            case 5 -> "V";
            case 6 -> "VI";
            case 7 -> "VII";
            case 8 -> "VIII";
            case 9 -> "IX";
            default -> "";
        };
    }

    private String transitTenPlace(int n) {
        return switch (n) {
            case 1 -> "X"; //10
            case 2 -> "XX";
            case 3 -> "XXX";
            case 4 -> "XL";
            case 5 -> "L";
            case 6 -> "LX";
            case 7 -> "LXX";
            case 8 -> "LXXX";
            case 9 -> "XC";//90
            default -> "";
        };
    }

    private String transitHandredPlace(int n) {
        return switch (n) {
            case 1 -> "C"; //10
            case 2 -> "CC";
            case 3 -> "CCC";
            case 4 -> "CD";
            case 5 -> "D";
            case 6 -> "DC";
            case 7 -> "DCC";
            case 8 -> "DCCC";
            case 9 -> "CM";//90
            default -> "";
        };
    }

    private String transitThousandPlace(int n) {
        return switch (n) {
            case 1 -> "M"; //10
            case 2 -> "MM";
            case 3 -> "MMM";
            default -> "";
        };
    }
}
